package com.jinzecloud.fiction.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jinzecloud.fiction.bean.TbUser;
import com.jinzecloud.fiction.constant.UserConstant;
import com.jinzecloud.fiction.service.TbUserService;
import com.jinzecloud.fiction.utils.PageUtils;
import com.jinzecloud.fiction.utils.Query;
import com.jinzecloud.fiction.vo.TbAuthorVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.jinzecloud.fiction.mapper.TbAuthorMapper;
import com.jinzecloud.fiction.bean.TbAuthor;
import com.jinzecloud.fiction.service.TbAuthorService;


@Service("tbAuthorService")
public class TbAuthorServiceImpl extends ServiceImpl<TbAuthorMapper, TbAuthor> implements TbAuthorService {

    @Autowired
    TbUserService tbUserService;
    @Autowired
    TbAuthorMapper tbAuthorMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<TbAuthor> tbAuthorQueryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            tbAuthorQueryWrapper.eq("au_id", key);
        }
        // TODO 作者列表根据用户名称查询
//        if (!StringUtils.isEmpty(key)){
//            tbAuthorQueryWrapper.or().like("au_id",key);
//        }
        if (!StringUtils.isEmpty(key)) {
            tbAuthorQueryWrapper.or().like("au_pen_name", key);
        }
        if (!StringUtils.isEmpty(key)) {
            tbAuthorQueryWrapper.or().like("au_introduce", key);
        }
        IPage<TbAuthor> page = this.page(new Query<TbAuthor>().getPage(params), tbAuthorQueryWrapper);

        PageUtils pageUtils = new PageUtils(page);
        List<TbAuthor> records = page.getRecords();
        List<Object> list = records.stream().map(tbAuthor -> {
            TbAuthorVo tbAuthorVo = new TbAuthorVo();
            TbUser tbUser = tbUserService.getById(tbAuthor.getAuUserId());
            tbAuthorVo.setUsUserName(tbUser.getUsUserName());
            BeanUtils.copyProperties(tbAuthor, tbAuthorVo);
            return tbAuthorVo;
        }).collect(Collectors.toList());
        pageUtils.setList(list);
        return pageUtils;
    }

    @Override
    public void saveTbAuthor(TbAuthor tbAuthor) {
        tbAuthor.setAuCreationTime(new Date());
        tbAuthor.setAuUpdateTime(new Date());
        tbAuthor.setAuStatus("1");
        tbAuthorMapper.insert(tbAuthor);
    }

    @Override
    public void updateTbAuthorById(TbAuthor tbAuthor) {
        tbAuthor.setAuUpdateTime(new Date());
        UpdateWrapper<TbAuthor> tbAuthorUpdateWrapper = new UpdateWrapper<>();
        tbAuthorUpdateWrapper.eq("au_id", tbAuthor.getAuId()).set("au_user_id", tbAuthor.getAuUserId()).set("au_introduce", tbAuthor.getAuPenName()).set("au_update_time", tbAuthor.getAuUpdateTime());
        tbAuthorMapper.update(null, tbAuthorUpdateWrapper);
    }

    @Override
    public void updateTbAuthorStatus(TbAuthor tbAuthor) {
        UpdateWrapper<TbAuthor> tbAuthorUpdateWrapper = new UpdateWrapper<>();
        tbAuthorUpdateWrapper.eq("au_id", tbAuthor.getAuId()).set("au_status", tbAuthor.getAuStatus());
        tbAuthorMapper.update(null, tbAuthorUpdateWrapper);
    }

}